package com.hhc.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应结果封装类
 *
 * @author pc
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class RestResult<T> {

    private String code;

    private String msg;

    private Object data;

    public static <T> RestResult<T> success(T data) {
        RestResult<T> result = new RestResult<>();
        result.setCode("000000");
        result.setMsg("调用成功");
        result.setData(data);
        return result;
    }

    /**
     * 失败响应构造器
     *
     * @param msg 错误信息
     * @return 失败的结果对象
     */
    public static <T> RestResult<T> failure(String msg) {
        return new RestResult<>("000001", msg, null);
    }

    public static RestResult success(String msg) {
        return new RestResult("000000", msg, null);
    }

    public static <T> RestResult<T> error(String code, String msg) {
        RestResult<T> result = new RestResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> RestResult<T> ok(T data) {
        return new RestResult<>("000000", "调用成功", data);
    }

    public static RestResult success(String msg, Object data) {
        return new RestResult("000000", msg, data);
    }

    public static RestResult fail(String code, String msg) {
        return new RestResult(code, msg, null);
    }

    public static RestResult fail(String code, String msg, Object data) {
        return new RestResult(code, msg, data);
    }

    public static <T> RestResult<T> success() {
        return success(null);
    }

    public static <T> RestResult<T> failed(String msg) {
        RestResult<T> result = new RestResult<>();
        result.setCode("000001");
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

    public static <T> RestResult<T> failed(String code, String msg) {
        RestResult<T> result = new RestResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
}
